﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.Xml.Linq;
using System.Windows;
using System.Collections.ObjectModel;

namespace WpfApplication1.Models
{
    [Serializable]
    public class Ordre : INotifyPropertyChanged
    {
        // INotifyPropertyChanged Member
        public event PropertyChangedEventHandler PropertyChanged;
        void Notify(string propName) {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }

        // Fields
        private DateTime ordreDato = DateTime.Today;
        private int ordreReference = 561978;
        private String bestiltAf;
        private List<OrdreItem> ordreItem;

        //Constructors

        public Ordre() { }

        public Ordre(DateTime ordredato, int ordrereference, String bestiltAf, List<OrdreItem> ordreItem)
        {
            this.Ordredato = ordredato;
            this.Ordrereference = ordrereference;
            this.BestiltAf = bestiltAf;
            this.OrdreItem = ordreItem;
        }


        //Properties

        public DateTime Ordredato
        {
            get { return ordreDato; }
            set { 
                ordreDato = value;
                Notify("OrdreDato");
            }
        }
        
        public int Ordrereference
        {
            get { return ordreReference; }
            set { 
                ordreReference = value;
                Notify("Ordrereference");
            }
        }
        
        public String BestiltAf
        {
            get { return bestiltAf; }
            set { 
                bestiltAf = value;
                Notify("BestiltAf");
            }
        }
        
        public List<OrdreItem> OrdreItem
        {
            get { return ordreItem; }
            set {
                ordreItem = value;
                Notify("OrdreItem");
            }
        }

        
        //Methods

        public void addItem(OrdreItem item)
        {
            OrdreItem.Add(item);
        }

        public String ordreToXml(Ordre ordre)
        {
            String xml;
            xml = SerializeObjectToXmlString(ordre);
            return xml;
        }

        /*
        /// <summary>
        /// Indsætter xml i enden af xml med namespaces
        /// </summary>
        /// <param name="obj"></param>
        public void insertOrdre(Ordre obj)
        {
            // Indlæs xml dokument
            XElement loadXML = XElement.Load("../../Data/Ordrer.xml");
            // Indsætter xml i enden af fil, desværre indgår namespace også
            XElement o = XElement.Parse(ordreToXml(obj));
            loadXML.Add(o);
            loadXML.Save("../../Data/Ordrer.xml");
        }
         */

        /// <summary>
        /// Indsætter xml i enden af xml dokument uden namespaces
        /// </summary>
        /// <param name="obj"></param>
        public void insertOrdre(Ordre obj, String filePath)
        {
            // Indlæs xml dokument
            XElement loadXML = XElement.Load(filePath);

            // Indsætter elementer manuelt, men problemer med at få ordreitem med ind
            XElement ordre = new XElement("Ordre",
                new XElement("Ordredato", obj.Ordredato.ToString("dd-MM-yyyy")),
                new XElement("Ordrereference", obj.Ordrereference),
                new XElement("BestiltAf", obj.BestiltAf),                
                    from c in obj.OrdreItem
                    select
                    new XElement("OrdreItem",
                    new XElement("Produktkode", c.ProduktKode),
                    new XElement("ProduktNavn", c.ProduktNavn),
                    new XElement("KalibreringsDato", c.KalibreringsDato.ToString("dd-MM-yyyy")),
                    new XElement("LeveringsDato", c.LeveringsDato.ToString("dd-MM-yyyy")),
                    new XElement("Antal", c.Antal)));
            loadXML.Add(ordre);
            loadXML.Save(filePath);
        }

        public void getOrdrer(String filePath)
        {
            //List<Ordre> o;

            XDocument xml = XDocument.Load(Properties.Settings.Default.OrdreDokument);
            //var query = from c in XElement.Load(filePath).Elements("Ordre")
            //            select c;

            //XElement xml = XElement.Load(filePath);
            //var query1 = from c in xml
            //             select c;

            var queryResult = from c in xml.Descendants("Ordre") select c.Elements();
            foreach (var item in queryResult)

            foreach (var c in queryResult)
            {
                MessageBox.Show(c.ToString());
            }
            //return o;
        }

        private static string SerializeObjectToXmlString<T>(T obj)
        {
            XmlSerializer xmls = new XmlSerializer(typeof(T));

            using (MemoryStream ms = new MemoryStream())
            {
                XmlWriterSettings settings = new XmlWriterSettings();

                settings.Encoding = Encoding.GetEncoding("ISO-8859-1");
                settings.Indent = true;
                settings.OmitXmlDeclaration = true;
                settings.IndentChars = "\t";
                settings.NewLineChars = Environment.NewLine;
                settings.ConformanceLevel = ConformanceLevel.Document;

                using (XmlWriter writer = XmlTextWriter.Create(ms, settings))
                {
                    xmls.Serialize(writer, obj);
                }

                string xml = Encoding.UTF8.GetString(ms.ToArray());

                return xml;
            }
        }
    }
}
